using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using IRQ.BaseFunctions;
using System.Xml.Serialization;

namespace SimpleGPSApp.Base
{
    public class ApplicationHelper
    {
        public static string StartupPath
        {
            get
            { 
                return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            }
        }

        public static object locker = new object();

        public static bool SaveTxtFile(string path, FileMode mode, string message, bool _writeLine)
        {
            lock (locker)
            {
                using (FileStream fs = new FileStream(path, mode, FileAccess.Write, FileShare.ReadWrite))
                {
                    if (fs == null)
                    {
                        string errorMessage = string.Join(string.Empty, new []{"SaveTxtFile: Error opening file stream for: ", path});
                        ErrorLog.WriteError(Root.ErrorLogFileName, errorMessage);
                        return false;
                    }

                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        if (sw == null)
                        {
                            ErrorLog.WriteError(Root.ErrorLogFileName, "SaveTxtFile: Failed to initialize stream");
                            return false;
                        }
                        try
                        {
                            if (_writeLine)
                            {
                                sw.WriteLine(message);
                            }
                            else
                            {
                                sw.Write(message);
                            }
                            return true;
                        }
                        catch (Exception e)
                        {
                            ErrorLog.WriteError(Root.ErrorLogFileName, "Exception: " + e.ToString());
                            return false;
                        }
                    }
                }
            }
        }

        public static void SaveXmlFile<T>(T data, string fileName)
        {
            using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
            {
                if (fileStream != null)
                {
                    try
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(T));
                        serializer.Serialize(fileStream, data);
                    }
                    catch (InvalidOperationException e)
                    {
                        ErrorLog.WriteError(Root.ErrorLogFileName, e.ToString());
                    }
                }
                else
                {
                    ErrorLog.WriteError(Root.ErrorLogFileName, "File creating error");
                }
            }
        }

        public static void LoadXmlFile<T>(ref T data, string fileName)
        {
            if (File.Exists(fileName))
            {
                using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
                {
                    if (fileStream != null)
                    {
                        try
                        {
                            XmlSerializer serializer = new XmlSerializer(typeof(T));
                            T tmp = (T)serializer.Deserialize(fileStream);

                            if (tmp != null)
                            {
                                data = tmp;
                            }
                            else
                            {
                                ErrorLog.WriteError(Root.ErrorLogFileName, string.Format("Error while deserializing file {0}", fileName));
                            }
                        }
                        catch (Exception e)
                        {
                            ErrorLog.WriteError(Root.ErrorLogFileName, e.ToString());
                        }
                    }
                    else
                    {
                        ErrorLog.WriteError(Root.ErrorLogFileName, "File opening error");
                    }
                }
            }
            else
            {
                ErrorLog.WriteError(Root.ErrorLogFileName, string.Format("File {0} doesn't exist", fileName));
            }
        }
    }
}
